package com.learncloud.netty.demo;

import io.netty.util.HashedWheelTimer;
import io.netty.util.Timeout;
import io.netty.util.Timer;
import io.netty.util.TimerTask;

import java.util.Date;
import java.util.concurrent.TimeUnit;

/**
  * @description TODO
  * @author lbx
  * @date 2021/10/7 13:36
  * @version 1.0.0
 **/
public class HashedWheelTimerTest {


    public static void main(String[] args) {
        Timer timer = new HashedWheelTimer();

        Timeout timeout = timer.newTimeout(new TimerTask() {
            @Override
            public void run(Timeout timeout) throws Exception {
                System.out.println("timeout1"+new Date());
            }
        },10,TimeUnit.SECONDS);

        if(!timeout.isExpired()){
            timeout.cancel();
        }

        timer.newTimeout(new TimerTask() {
            @Override
            public void run(Timeout timeout) throws Exception {
                System.out.println("timeout2: " + new Date());
                Thread.sleep(5000);
            }
        },1,TimeUnit.SECONDS);

        timer.newTimeout(new TimerTask() {
            @Override
            public void run(Timeout timeout) throws Exception {
                System.out.println("timeout3: " + new Date());
            }
        },3,TimeUnit.SECONDS);
    }
}
